//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Globals.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
THexForm *HexForm;
//---------------------------------------------------------------------------
__fastcall THexForm::THexForm(TComponent* Owner)
 : TForm(Owner)
{
}

//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::DrawHex( BYTE *String )
{
  WORD x,
       y,
       ptr,
       fnHeight;
  char buf[16];

  if ( !rom.data ) return;

  memset( hxData, 0, 0x1000 );

  // GET the BYTE count
  hexx.posi = 0;
  count     = 0;
  // 0xFF needs to be whatever the Text End value is
  while( count < 0x1000 && String[count] != 0xFF )
  { //
    hxData[ count ] = String[ count ];
    count++;
    if ( String[ count ] == 0xFF ){
      hxData[ count ] = String[ count ];
      count++;
      break;
    }
  }

  // 0xFF needs to be whatever the Text End value is
  if ( !count && String[ count ] == 0xFF ){
    hxData[ count ] = String[ count ];
    count++;
  }

  //
  fnHeight = abs( HexForm->Font->Height ) + 2;
  //
  hexx.mx = 16;
  hexx.my = (HexForm->ClientHeight / fnHeight);
  //
  y   = -1;
  while( ++y < hexx.my )
  { //
    ptr = 0;
    x = -1;
    while( ++x < hexx.mx )
    { //
      sprintf( buf, "%02X", hxData[ (y << 4) + x ] );
      //
      if ( x == hexx.x && y == hexx.y )
      { //
        HexForm->Canvas->Font->Color = clRed;
        SetBkColor( HexForm->Canvas->Handle, clWhite);
      }
      else
      { //
        HexForm->Canvas->Font->Color = ( x & 1 ) ? clWhite : clTeal;
        SetBkColor( HexForm->Canvas->Handle, clBlack );
      }

      //
			 HexForm->Canvas->TextOut( ptr, (y * fnHeight), buf );
      ptr += HexForm->Canvas->TextWidth( AnsiString(buf) );
    }
  }
  //
  DrawPic( );
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::UpdateHex( BYTE *String )
{
  WORD x,
       y,
       ptr,
       fnHeight;
  char buf[16];

  if ( !rom.data ) return;
  //
  fnHeight = abs( HexForm->Font->Height ) + 2;
  //
  hexx.mx = 16;
  hexx.my = (HexForm->ClientHeight / fnHeight);
  // Re-adjust the window height
//  HexForm->ClientHeight = (hexx.my * fnHeight);
  //
  y   = -1;
  while( ++y < hexx.my )
  { //
    ptr = 0;
    x = -1;
    while( ++x < hexx.mx )
    { //
      sprintf( buf, "%02X", hxData[ hexx.posi + (y << 4) + x ] );
      //
      if ( x == hexx.x && y == hexx.y )
      { //
        HexForm->Canvas->Font->Color = clRed;
        SetBkColor( HexForm->Canvas->Handle, clWhite);
      }
      else
      { //
        HexForm->Canvas->Font->Color = ( x & 1 ) ? clWhite : clTeal;
        SetBkColor( HexForm->Canvas->Handle, clBlack );
      }

      //
			 HexForm->Canvas->TextOut( ptr, (y * fnHeight), buf );
      ptr += HexForm->Canvas->TextWidth( AnsiString(buf) );
    }
  }
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::FormCreate(TObject *Sender)
{
  hexx.mx = 0;
  hexx.my = 0;
  hexx.x  = 0;
  hexx.y  = 0;

  PIC = new Graphics::TBitmap;

  PIC->Width       = 8;
  PIC->Height      = 8;
  PIC->PixelFormat = pf16bit;
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
  switch( Key )
  {
    case VK_LEFT : if ( hexx.x > 0 ) hexx.x--; else { hexx.x = (hexx.mx-1); goto _Up; }   break;
    case VK_RIGHT: if ( hexx.x < (hexx.mx-1) ) hexx.x++; else { hexx.x = 0; goto _Down; } break;
    //------------
    case VK_UP   : _Up:
                    if ( hexx.y > 0 ){ hexx.y--; break; }
                    if ( !hexx.y  && hexx.posi ) hexx.posi -= hexx.mx;
                    break;
    //------------
    case VK_DOWN : _Down:
                  if ( hexx.y < (hexx.my-1) ){ hexx.y++; break; }
                  if ( hexx.y == (hexx.my-1) && (hexx.posi + hexx.mx) < 0x1000 ) hexx.posi += hexx.mx;
                  break;
    //----------------------
    default : return;
  }
  //
  UpdateHex( hxData );
  DrawPic( );
}


//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::DrawPic( )
{
  LONG posi;
  BYTE TILE[8][8];
  WORD *VRAM;
  BYTE BUFR[0x20],
       flag = 0;

  if ( !rom.data || !PIC ) return;

  posi = hxData[ hexx.posi + (hexx.y << 4) + hexx.x];
  memset( (BYTE *)TILE, 0, 64 );
  //
  switch( posi )
  {
    case 0xCC:
    case 0xFB:
    case 0xFC:
    case 0xFD:
    case 0xFF: posi  = 0x7F; break;
    default  : posi &= 0x7F; break;
  }

  //----------------//
  // FONT is 16x16  /
  //----------------//
  posi  = (posi << 4) ;
  memcpy( BUFR, &rom.data[ map.lpFontOffset + posi ], 0x10 );
  // Convert the tile data
  int x = -1;
  while( ++x < 8 )
    Dec2Bin( TILE[ x ], BUFR[ x + 8 ], 8, 2 );

  //---------------//
  // DMA to Vram   //
  //---------------//
  for ( int y = 0; y < 8; y++ )
  { //
    (void *)VRAM = PIC->ScanLine[ y ];
    for ( int x = 0; x < 8; x++ )
      VRAM[ x ] = DOS565[ TILE[ y ][ x ] * 0x0F ];
  }
  //
  chPic->Canvas->StretchDraw( chPic->ClientRect, PIC );
  //
  // Display the Offset
  if ( (hexx.posi + (hexx.y << 4) + hexx.x) < (count-1) )
    HexForm->Caption = "Hex Data - $" + IntToHex( txt.pntr + ((hexx.posi + (hexx.y << 4) + hexx.x)), 8 );
  else
    HexForm->Caption = "Hex Data - $" + IntToHex( txt.pntr + (count-1), 8 );
}

//------------------------------------//
//                                    //
//------------------------------------//
void __fastcall THexForm::FormPaint(TObject *Sender)
{
  UpdateHex( hxData );
  DrawPic( );
}


void __fastcall THexForm::FormResize(TObject *Sender)
{
  FormPaint( Sender ); 
}
//---------------------------------------------------------------------------

